﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using MediaWiki.Common;
using MediaWiki.Office.Model.Utilities;

namespace MediaWiki.Office.Model
{
  public enum ImagePosition
  {
    Inline,
    Floating
  }

  public class Image : RunContent
  {
    public const string DrawingMLLocalName = "drawing";
    public const string VmlLocalName = "pict";

    private Unit _width;
    private Unit _height;
    private string _imageId;
    private string _name;
    private string _description;
    private string _hyperlinkId;

    public ImagePosition Position { get; set; }

    public Uri ImageUri
    {
      get { return GetResolvedTarget(_imageId); }
    }

    public string ImageId
    {
      get { return _imageId; }
      set { _imageId = value; }
    }

    public Unit Width
    {
      get { return _width; }
      set { _width = value; }
    }

    public Unit Height
    {
      get { return _height; }
      set { _height = value; }
    }


    public override RunContentType ContentType
    {
      get { return RunContentType.Image; }
    }

    public string Description
    {
      get { return _description; }
      set { _description = value; }
    }

    public string Name
    {
      get { return _name; }
      set { _name = value; }
    }

    public string HyperlinkId
    {
      get { return _hyperlinkId; }
      set { _hyperlinkId = value; }
    }

    public Uri HyperlinkUri
    {
      get { return GetTarget(_hyperlinkId); }
    }

    public Image(Run parent, XElement element)
      : base(parent, element)
    {
      XNamespace ns = Document.WordprocessingDrawingML;

      XElement container = element.Element(ns + "inline");
      if (container == null)
      {
        container = element.Element(ns + "anchor");
        Position = ImagePosition.Floating;
      }
      else
      {
        Position = ImagePosition.Inline;
      }

      _width = Unit.Emu(XmlUtils.GetValue<double>(container.Element(ns + "extent"), XName.Get("cx"))).Value;
      _height = Unit.Emu(XmlUtils.GetValue<double>(container.Element(ns + "extent"), XName.Get("cy"))).Value;

      XElement graphicElement = container.Element(Document.DrawingML + "graphic");
      XElement graphicDataElement = graphicElement.Element(Document.DrawingML + "graphicData");
      XElement picElement = graphicDataElement.Element(Document.PictureDrawingML + "pic");

      XElement fillElement = picElement.Element(Document.PictureDrawingML + "blipFill");

      _imageId = XmlUtils.GetValue(fillElement.Element(Document.DrawingML + "blip"), Document.Relationship + "embed");

      XElement nvPicPrElement = picElement.Element(Document.PictureDrawingML + "nvPicPr");
      if (nvPicPrElement != null)
      {
        XElement cNvPrElement = nvPicPrElement.Element(Document.PictureDrawingML + "cNvPr");
        _name = XmlUtils.GetValue(cNvPrElement, (XName)"name");
        _description = XmlUtils.GetValue(cNvPrElement, (XName)"descr");
      }

      XElement docPrElement = container.Element(ns + "docPr");
      if (docPrElement != null)
      {
        _hyperlinkId = XmlUtils.GetValue(docPrElement.Element(Document.DrawingML + "hlinkClick"), Document.Relationship + "id");
      }
    }

    public override string ToString()
    {
      return "{{Image: {0}}}".FormatWith(Name);
    }
  }
}